﻿using System;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.IO;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.IO;

using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.System;

using GPSController;
using Ascended.System;

namespace GPS_Module
{
    public class Program
    {
        enum GLL_Command { TYPE, LAT, NS, LONG, EW, TIME, STATUS, MODE, CHECKSUM, END };
        static readonly string GLL_HEADER = "$GPGLL";

        public static void Main()
        {

            SerialPort port = new SerialPort("COM1", 57600, Parity.None, 8, StopBits.One);

            LS20031 gps = new LS20031(port);

            gps.Init_GPS();
           
            //Debug.Print("Polling...");
            for (int i = 0; i < 15; i++)
            {
                while (!gps.Poll_GPS(Output_Data)) ;
                Thread.Sleep(750);
            }
            
        }

        private static void Output_Data(byte[] output)
        {
            char[] outs = new char[output.Length];
            char[] newouts = new char[256];
            float Longitude, Latitude,Time;
            int hour, min, sec;
            char NorSow, EasWes;

            string[] Commands = new string[8];

            outs = Encoding.UTF8.GetChars(output);

            if (outs[0] == ('G') | outs[0] == ('P')) //Checks to see if the '$' was eatten and adds it back to the front
            {
                string check1 = new string(outs);
                check1 = String.Concat('$', check1);
                outs = check1.ToCharArray();
            }

            
            string NMEA = new string(outs);
            string[] final = NMEA.Split(new char[] { });

            int i = 0;
            foreach (string command in final)
                if (command.Trim() != "")
                {
                    //Debug.Print(command);
                    Commands[i] = command;
                    i++;
                }
            
            for(int j = 0; j<i;j++)
            {
                string[] parse = Commands[j].Split(new char[] {','});

                if (parse[(int)GLL_Command.TYPE].CompareTo(GLL_HEADER) == 0)
                {
                    Debug.Print("\n-------------------");

                    Latitude = (ConvertString.ToSingle(parse[(int)GLL_Command.LAT]));
                    //Debug.Print("LAT: " + (Latitude).ToString());
                    Latitude /= 100;
                    Latitude = (float)((int)(Latitude) + (Latitude - (int)Latitude)*((float)10/6));
                   // Debug.Print("LAT: "+ (Latitude).ToString()+" degrees");

                    char[] NS = parse[(int)GLL_Command.NS].ToCharArray();
                    NorSow = NS[0];
                    Debug.Print("Latitude: " + Latitude.ToString() + " " + NorSow.ToString());

                    Longitude = ConvertString.ToSingle(parse[(int)GLL_Command.LONG]);
                    //Debug.Print("LONG: " + Longitude.ToString());
                    Longitude /= 100;
                    Longitude = (float)((int)(Longitude) + (Longitude - (int)Longitude) * ((float)10 / 6));
                    //Debug.Print("LONG: " + (Longitude).ToString() + " degrees");

                    char[] EW = parse[(int)GLL_Command.EW].ToCharArray();
                    EasWes = EW[0];
                    Debug.Print("Longitude: " + Longitude.ToString()+ " " + EasWes.ToString());

                    Time = ConvertString.ToSingle(parse[(int)GLL_Command.TIME]);
                    hour = (int)(Time / 10000);
                    min = (int)((Time - (hour * 10000)) / 100);
                    sec = (int)(Time - (hour * 10000) - (min * 100));
                    //Debug.Print("Time: " + Time.ToString());
                    if (sec < 10)
                    {
                        Debug.Print("Time: " + hour + ":" + min + ":0" + sec);
                    }
                    else Debug.Print("Time: " + hour + ":" + min + ":" + sec);
                    Debug.Print("-------------------\n");
 
                }
            }
        }   


    }
}
